var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    times: ['累计', '今日', '本周', '本月', '本年'],
    page: 1,
    limit: 100,
    currentTab: 0,
    total_page: 0,
    saleltList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 隐藏分享
    wx.hideShareMenu()
  },
  onShow: function() {

    this.setData({
      page: 1,
      currentTab: 0
    })
    this.customerListGet();
    this.saleStatistics();


  },


  //客户列表
  customerListGet: function() {
    var that = this;
    var page = this.data.page;
    var time = this.data.currentTab;
    var limit = this.data.limit;
    var params = {
      limit: that.data.limit,
      time: time,
      page: page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'inn/customerList',
      success: function(res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var customerList = res.data.data.list;
          for (let i = 0; i < customerList.length; i++) {
            customerList[i].select = 0
          }
          if (page == 1) {
            var itemData = customerList;
          } else {
            var itemData = that.data.customerList.concat(customerList)
          }

          that.setData({
            count: res.data.data.paging.count,
            total_page: totalPage,
            customerList: itemData
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        util.showError("网络错误，请检查网络");

      }
    })
  },

  selectCustomer: function(e) {
    // console.log(e.currentTarget.dataset.index);
    var list = this.data.customerList;
    var index = e.currentTarget.dataset.index;
    for (let i = 0; i < list.length; i++) {
      if (i == index) {
        list[i].select = (list[i].select==1?0:1)
      } else {
        list[i].select = 0
      }
    }

    this.setData({
      customerList: list
    })
  },

  //销量统计
  saleStatistics: function() {
    var that = this

    api.POST({
      params: {
        time: 0
      },
      url: app.globalData.base_url + 'inn/saleStatistics',
      success: function(res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            saleAll: res.data.data.all
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        util.showError("网络错误，请检查网络");

      }
    })

  },


  swichNav: function(e) {
    this.setData({
      currentTab: e.target.dataset.currentTime,
      page: 1
    })

    this.customerListGet();

  },


  onShareAppMessage: function(res) {
    var that = this;
    var nickname = wx.getStorageSync('accountInfo').nickname;
    var inn_id = wx.getStorageSync('accountInfo').inn_id;
    var url = '/agent/pages/storeGoodsList/storeGoodsList?inn_id=' + inn_id;
    var share_img = app.globalData.base_url + 'images/share_01.jpg'
    var title = nickname + '向你分享了ta的小店'
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: title,
        path: url,
        imageUrl: share_img,
        success: function(res) {
          // 转发成功
        },
        fail: function(res) {
          // 转发失败
        }
      }
    }
  },



  //页面下拉刷新
  onPullDownRefresh: function() {
    this.refresh();
  },

  //刷新数据
  refresh: function() {
    var that = this;
    that.setData({
      page: 1,
    });
    setTimeout(function() {
      that.customerListGet()
    }, 500)
  },

  //页面上拉触底
  onReachBottom: function(e) {
    this.loadMore();
  },

  //加载更多
  loadMore: function() {
    var that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      page: this.data.page + 1
    });
    setTimeout(function() {
      that.customerListGet()
    }, 500)
  },

  //推送小店
  sendShop: function(e) {
    var uid = e.currentTarget.dataset.uid;
    api.POST({
      params: {
        uid: uid
      },
      url: app.globalData.base_url + 'inn/sendInn',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          util.showError('推送成功')

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        util.showError("网络错误");

      }
    })
  },

  //推送商品
  sendGoods: function(e) {
    var uid = e.currentTarget.dataset.uid;
    wx.navigateTo({
      url: '/agent/pages/sendGoods/sendGoods?uid='+uid,
    })
  }

})